package cn.lugege.org.service.impl;

import cn.lugege.basic.service.impl.BaseServiceImpl;
import cn.lugege.basic.util.PageList;
import cn.lugege.org.domain.Department;
import cn.lugege.org.mapper.DepartmentMapper;
import cn.lugege.org.query.DepartmentQuery;
import cn.lugege.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void add(Department d) {
        departmentMapper.save(d);
        this.upadte(d);
    }

    @Override
    @Transactional
    public void upadte(Department d) {
        //第一 如果我的parent为空的时候说明没有父部门  那dir存的就是自己
        //2.如果有parent 那就去找对应的对象 定位到dirpath
        if(d.getParent()==null){
            d.setDirPath("/"+d.getId());
        }else{
            Department dbDepartment = departmentMapper.loadById(d.getParent().getId());
            d.setDirPath(dbDepartment.getDirPath()+"/"+d.getId());
        }
        departmentMapper.update(d);
    }

    /**
     * 部门树
     * @return
     */
    @Override
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }
}
